package com.cqrcb.cloud.utils;


import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import com.cqrcb.cloud.properties.SftpServerProperties;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @Author bq8023
 * @Date 21/5/9 19:57
 * @Version 1.0
 */
@Slf4j
public class SshCommand implements Callable<StringBuilder>{

    private InputStream in;
    private int maxLength;

    public SshCommand(InputStream in, int maxLength) {
        this.in = in;
        this.maxLength = maxLength;
    }

    @Override
    public StringBuilder call() {
        byte[] buf = new byte[1024];
        StringBuilder builder = new StringBuilder();
        try {
            int length;
            int count = maxLength;
            while ((length = in.read(buf)) != -1) {
                String str = new String(buf, 0, length);
                log.info(str);
                builder.append(str);
                if(count != -1){
                    if(-- maxLength <= 0){
                        break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder;
    }
}

